package com.examstack.portal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.examstack.common.domain.exam.UserQuestionCollect;
import com.examstack.common.domain.practice.Collect;
import com.examstack.common.domain.question.QuestionStatistic;
import com.examstack.portal.persistence.QuestionCollectMapper;

@Service("questionCollectService")
public  class QuestionCollectServiceImpl implements QuestionCollectService {

	@Autowired
	private QuestionCollectMapper questionCollectMapper;

	@Override
	public void addUserCollect(Collect co) {

		questionCollectMapper.insertCollect(co.getUser_id(), co.getQuestion_id(), co.getQuestion_type_id());

	}

	@Override
	public void addUserQuestionCollect(UserQuestionCollect... history) {

		List<UserQuestionCollect> ls = new ArrayList<UserQuestionCollect>();

		for (UserQuestionCollect h : history) {
			ls.add(h);
		}

		questionCollectMapper.addUserQuestionCollect(ls);

	}

	@Override
	public Collect Collect(int userid, int questionid) {
		// TODO Auto-generated method stub
		return questionCollectMapper.getCollect(userid, questionid);
	}

	@Override
	public void updUserCollect(int userid, int questionid, String isCollect) {
		// TODO Auto-generated method stub
		questionCollectMapper.updCollect(userid, questionid, isCollect);
	}


	@Override
	public Map<Integer, List<UserQuestionCollect>> getUserQuestionCollect(int userId, int fieldId) {
		// TODO Auto-generated method stub

		List<UserQuestionCollect> histList = questionCollectMapper.getUserQuestionCollect(userId, fieldId);
		Map<Integer, List<UserQuestionCollect>> map = new HashMap<Integer, List<UserQuestionCollect>>();
		for (UserQuestionCollect hist : histList) {
			List<UserQuestionCollect> result = map.get(hist.getPointId());
			if (result == null)
				result = new ArrayList<UserQuestionCollect>();

			result.add(hist);
			map.put(hist.getPointId(), result);
		}
		return map;
	}

	@Override
	public Map<Integer, QuestionStatistic> getQuestionCollectStaticByFieldId(int fieldId, int userId) {

		List<QuestionStatistic> statisticList = questionCollectMapper.getQuestionCollectStaticByFieldId(fieldId,
				userId);
		Map<Integer, QuestionStatistic> map = new HashMap<Integer, QuestionStatistic>();
		for (QuestionStatistic statistic : statisticList) {
			map.put(statistic.getPointId(), statistic);
		}

		return map;

	};

	/**
	 * 根据fieldId,pointId,typeId分组统计练习收藏试题数量
	 * 
	 * @param fieldId
	 * @param userId
	 * @return
	 */
	@Override
	public Map<Integer, Map<Integer, QuestionStatistic>> getTypeQuestionCollectStaticByFieldId(int fieldId,
			int userId) {
		List<QuestionStatistic> statisticList = questionCollectMapper.getTypeQuestionCollecttStaticByFieldId(fieldId,
				userId);
		Map<Integer, Map<Integer, QuestionStatistic>> map = new HashMap<Integer, Map<Integer, QuestionStatistic>>();
		for (QuestionStatistic statistic : statisticList) {
			Map<Integer, QuestionStatistic> tmp = map.get(statistic.getPointId());
			if (tmp == null) {
				tmp = new HashMap<Integer, QuestionStatistic>();
			}
			tmp.put(statistic.getQuestionTypeId(), statistic);
			map.put(statistic.getPointId(), tmp);
		}
		return map;
	}

}
